package com.educationagent.config.chatMemory;

import dev.langchain4j.community.model.zhipu.ZhipuAiEmbeddingModel;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.pinecone.PineconeEmbeddingStore;

import dev.langchain4j.store.embedding.pinecone.PineconeServerlessIndexConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.time.Duration;

import static dev.langchain4j.community.model.zhipu.embedding.EmbeddingModel.EMBEDDING_3;
import static dev.langchain4j.community.model.zhipu.embedding.EmbeddingModel.TEXT_EMBEDDING;



@Configuration
public class MemoryChatAssistantConfig {


    private EmbeddingModel embeddingModel;

    private EmbeddingStore embeddingStore;

    // 使用 setter 注入
    @Autowired
    public void setEmbeddingStore(@Lazy EmbeddingStore embeddingStore) {
        this.embeddingStore = embeddingStore;
    }
    @Autowired
    public void setEmbeddingModel(@Lazy EmbeddingModel embeddingModel) {
        this.embeddingModel = embeddingModel;
    }



    @Bean
    public ChatMemoryProvider chatMemoryProvider() {
        //设置会话id，和聊天上下文记忆数
        return memoryId -> MessageWindowChatMemory.builder().id(memoryId).maxMessages(10).build();
    }

    /*@Bean
    public ContentRetriever myContentRetriever() {

        //文档加载
        Document document = FileSystemDocumentLoader.loadDocument("src/main/resources/static/1.pdf"
                ,new ApachePdfBoxDocumentParser());
        List<Document> list = Arrays.asList(document);

        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();

        //文档分割，转换向量，存入，基于本地内存
        EmbeddingStoreIngestor.ingest(list,embeddingStore);
        return EmbeddingStoreContentRetriever.from(embeddingStore);
    }*/

  /*  @Bean
    public EmbeddingModel embeddingModel() {
        return OpenAiEmbeddingModel.builder()
                .apiKey("sk-01ec2324dedf404bbbc64d3622366c74")
                .modelName("text-embedding") // DeepSeek要求的名称
                .baseUrl("https://api.deepseek.com/v1/embeddings")
                .timeout(Duration.ofSeconds(60))
                .dimensions(1024) // DeepSeek嵌入维度是1024
                .logRequests(true) // 调试时启用
                .logResponses(true)
                .build();
    }*/

    @Bean
    public EmbeddingModel embeddingModel() {
        return ZhipuAiEmbeddingModel.builder()
                .model(String.valueOf(TEXT_EMBEDDING))
                .apiKey("2aa0a075296a4c97bbe8fc61845aa56d.2250AdKJIKfv9nls")
                .callTimeout(Duration.ofSeconds(60))
                .connectTimeout(Duration.ofSeconds(10))
                .readTimeout(Duration.ofSeconds(30)) // 添加readTimeout配置
                .writeTimeout(Duration.ofSeconds(10)) // 添加writeTimeout配置
                .dimensions(1024)
                .logRequests(true)
                .logResponses(true)
                .maxRetries(1)
                .build();
    }

    /*初始化向量数据库*/
    @Bean
    public EmbeddingStore<TextSegment> embeddingStore() {
        PineconeEmbeddingStore embeddingStore = PineconeEmbeddingStore.builder()
                .apiKey("pcsk_3vUip7_BpfTHioCDxt6ddmqkrwqEC73uWqCT3j3XipGUcy4GjsfirYMaSUV9RdkK3G3MzQ")
                .index("agent")
                .nameSpace("agent-namespace")
                .createIndex(PineconeServerlessIndexConfig.builder()
                        .cloud("AWS")
                        .region("us-east-1")
                        //当使用本地模型上面的打开，当使用远程deepseek模型时开启下面的  需要删除远程想来数据库，因为向量维度不同
                        //.dimension(embeddingModel.dimension())
                        .dimension(1024)
                        .build())
                .build();
        return embeddingStore;
    }


    @Bean
    public ContentRetriever myContentRetriever(){
        return EmbeddingStoreContentRetriever.builder()
                .embeddingModel(embeddingModel)
                .embeddingStore(embeddingStore)
                .maxResults(1)
                .minScore(0.8)
                .build();
    }





}
